﻿<?php
if(!defined('IN_PHPSCUP'))exit('hacking'); 

class VoteApp extends BaseApp
{

    var $type = 'vote';
	
    function index()
    {
		
		
		$TB_links = &$this->_model('vote'); 		
		$data = $TB_links->show();
        $this->assign('lw_data', $data['data']);		
		$this->assign('lw_page', $data['page']);	
	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('vote_list.view.php');
    }

    function add()
    {
	
	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('vote_add.view.php');
    }	
	
    function edit()
    {
		
	    $cid = $_GET['cid'];	

	$votetpl0=read_file(ROOT_PATH."/data/votehtml/0.htm");
	$votetpl0=str_replace("\r\n","",$votetpl0);
	$votetpl0=str_replace("'","\'",$votetpl0);

	$votetpl1=read_file(ROOT_PATH."/data/votehtml/1.htm");
	$votetpl1=str_replace("\r\n","",$votetpl1);
	$votetpl1=str_replace("'","\'",$votetpl1);

	$votetpl2=read_file(ROOT_PATH."/data/votehtml/2.htm");
	$votetpl2=str_replace("\r\n","",$votetpl2);
	$votetpl2=str_replace("'","\'",$votetpl2);
        $db = & get_db();
	    $pre = DB_PREFIX;
	$rsdb=$db->get_one("SELECT * FROM `{$pre}vote_topic` WHERE cid='$cid'");
	$query = $db->query("SELECT * FROM `{$pre}vote_element` WHERE cid='$cid' ORDER BY list DESC");
	$i=0;
	while($rs = $db->fetch_array($query)){
		$i++;
		$listdb[$i]=$rs;
	}
	$nums=count($listdb);	

	    $votetype[$rsdb['votetype']]=' checked ';

        $this->assign('votetpl0',$votetpl0);		
        $this->assign('votetpl1',$votetpl1);		
        $this->assign('votetpl2',$votetpl2);
		
        $this->assign('votetype',$votetype);	
        $this->assign('nums',$nums);		
        $this->assign('data',$listdb + $rsdb);		
        $this->assign('listdb',$listdb);
        $this->assign('cid',$cid);	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('vote_edit.view.php');
    }




    function addpost()
    {

	     $data = $_POST['data'];
        $db = & get_db();
	    $pre = DB_PREFIX;
        $timestamp = time();		
	$data['begintime']&& $data['begintime']=strtotime($data['begintime']);
	$data['endtime']&& $data['endtime']=strtotime($data['endtime']);
	$db->query("INSERT INTO `{$pre}vote_topic` ( `name` , `about` , `type` , `limittime` , `limitip` , `ip` , `posttime` , `user` , `begintime` , `endtime` , `forbidguestvote` ) 
		VALUES (
		'$data[name]','$data[about]','$data[type]','$data[limittime]','$data[limitip]','','$timestamp','$data[user]','$data[begintime]','$data[endtime]','$data[forbidguestvote]')");
	$rs=$db->get_one("SELECT * FROM `{$pre}vote_topic` ORDER BY cid DESC LIMIT 1");
	unset($data['name'],$data['type'],$data['limittime'],$data['limitip'],$data['forbidguestvote'],$data['begintime'],$data['endtime'],$data['about']);
	foreach($data AS $key=>$value){
		$value[title]&&$db->query("INSERT INTO `{$pre}vote_element` (`cid` , `title` , `votenum`, `img` ) VALUES ('$rs[cid]', '$value[title]', '$value[votenum]', '$value[img]')");
	}
		 
		 $this->index();

    }

    function editpost()
    {

	     $data = $_POST['data'];
		 
		 $cid = intval($_POST['cid']);	

        $db = & get_db();
	    $pre = DB_PREFIX;
		$timestamp = time();
	foreach( $_COOKIE AS $key=>$value)
	{
		if(strstr($key,'vote_limittime')){
			setcookie($key,"0",$timestamp-3600,"/");
		}
	}
	if($data[type]<1){
		$data[type]=1;
	}
	$data['begintime']&& $data['begintime']=strtotime($data['begintime']);
	$data['endtime']&& $data['endtime']=strtotime($data['endtime']);	
	$tempdata = $data ;
	unset($data['name'],$data['type'],$data['limittime'],$data['limitip'],$data['forbidguestvote'],$data['begintime'],$data['endtime'],$data['about'],$data['votetype'],$data['votetpl']);
	
	if($tempdata[type]>count($data)){
		$tempdata[type]=count($data);
	}
 	$data = $tempdata ;   
	
	$db->query("UPDATE `{$pre}vote_topic` SET name='$data[name]',about='$data[about]',type='$data[type]',limittime='$data[limittime]',limitip='$data[limitip]',begintime='$data[begintime]',endtime='$data[endtime]',forbidguestvote='$data[forbidguestvote]',ifcomment='$data[VoteUseComment]',votetype='$data[votetype]',tplcode='$data[votetpl]' WHERE cid='$cid'");
	unset($data['name'],$data['type'],$data['limittime'],$data['limitip'],$data['forbidguestvote'],$data['begintime'],$data['endtime'],$data['about'],$data['votetype'],$data['votetpl']);	
	foreach($data AS $key=>$v){
		if($v[id]){
			$db->query("UPDATE `{$pre}vote_element` SET title='$v[title]',votenum='$v[votenum]',list='$v[list]',img='$v[img]',describes='$v[describes]',url='$v[url]' WHERE id='$v[id]'");
		}else{
			$v[title]&&$db->query("INSERT INTO `{$pre}vote_element` (`cid` , `title` , `votenum`, `img`, `describes`, `url` ) VALUES ('$cid', '$v[title]', '$v[votenum]', '$v[img]', '$v[describes]', '$v[url]')");
		}
	}
		 
		 $this->index();

    }


    function del()
    {

	     $cid = $_GET['cid'];
        $db = & get_db();
	    $pre = DB_PREFIX;		 
	$db->query("DELETE FROM `{$pre}vote_topic` WHERE cid='$cid'");
	$db->query("DELETE FROM `{$pre}vote_element` WHERE cid='$cid'");
	$db->query("DELETE FROM `{$pre}vote_comment` WHERE cid='$cid'");	 
		 $this->index();	  

    }
    function delvote()
    {
	    $id = $_GET['id'];
        $db = & get_db();
	    $pre = DB_PREFIX;		 
	    $db->query("DELETE FROM `{$pre}vote_element` WHERE id='$id'"); 
		$this->index();	  

    }

    function upfile()
    {
	 if(!isset($_POST["action"]))
	 {
	 $_POST["action"]='';
	 }
	 if(!isset($_GET['id']))
	 {
	 $_GET['id']='';
	 }
	 if(!isset($_GET["edit"]))
	 {
	 $_GET["edit"]='';
	 }	 
	 if(!isset($_GET["picurl"]))
	 {
	 $_GET["picurl"]='';
	 }	
	 if(!isset($_GET['jsfun']))
	 {
	 $_GET['jsfun']='';
	 }		 
	 if(!isset($_GET['putid']))
	 {
	 $_GET['putid']='';
	 }		 
	 if(!isset($_GET['uppic']))
	 {
	 $_GET['uppic']='';
	 }	
		$tf = $_GET['tf'];
        if(empty($tf))$tf = $_POST['tf'];	 	 	
		$action = $_POST["action"];		
		$id = intval($_GET['id']);		
		$jsfun = $_GET['jsfun'];
		$uppic = $_GET['putid'];
		if(!empty($_GET['uppic']))
		{
		$uppic = $_GET['uppic'];		
		}	
		$edit = $_GET["edit"];
		$picurl = $_GET["picurl"];

        $this->assign('jsfun', $jsfun);		


        $this->assign('uppic', $uppic);		
	
				

        $this->assign('edit', $edit);		


        $this->assign('picurl', $picurl);

        $this->assign('tf', $tf);
						
		if($action == 'post')
		{
			 if(!isset($_POST['jsfun']))
	 {
	 $_POST['jsfun']='';
	 }	
	 if(!isset($_POST['uppic']))
	 {
	 $_POST['uppic']='';
	 }		 
	 if(!isset($_POST["edit"]))
	 {
	 $_POST["edit"]='';
	 }		 
	 if(!isset($_POST["picurl"]))
	 {
	 $_POST["picurl"]='';
	 }	
	 if(!isset($_POST['id']))
	 {
	 $_POST['id']='';
	 }		 
		       $jsfun = $_POST['jsfun'];
		       $uppic = $_POST['uppic'];		
		       $edit = $_POST["edit"];
		       $picurl = $_POST["picurl"];
		 	   $id || $id = intval($_POST['id']);		
		       $filedname = trim(str_replace(array("UP_"),array(""),$uppic)); 
       
		       include(ROOT_PATH.'/lib/upfile.lib.php');	  
	           $upfile = new Upfile($this->type.'/'.$id); 	  
	           $updata = $upfile->_batch_upfile($this->type);		 
		 		 
		       $newfile = $updata[1]['path'];
	       
			   if($edit=='editp' && $picurl)
			   {

			          $TB_new = &$this->_model('content');			    		 		 		
		              $TB_new->editpicurl($id,array("$filedname"=>$newfile),$this->table_id);
		              $pic_dir= ROOT_PATH . '/' . UPFILE_PATH . '/' . $picurl;
			          if(!is_dir($pic_dir)&&file_exists($pic_dir))
		              {
		                del_file($pic_dir);	
		              }					  
				   
			   }

			    if($edit=='edit' && $picurl)
		       {
		              $pic_dir= ROOT_PATH . '/' . UPFILE_PATH . '/' . $picurl;
			          if(!is_dir($pic_dir)&&file_exists($pic_dir))
		              {
		                del_file($pic_dir);	
		              }				 
		       }
		
		      $pic_url = SITE_URL . '/' . UPFILE_PATH . '/' .$newfile;

              if($tf=='pic')
			  {
	           echo "<img src='{$pic_url}' id='upsrc' width='100px' height='100px'><br/><A HREF='?app=$this->type&act=upfile&edit=$edit&tf=$tf&picurl=$newfile&id=$id&jsfun=$jsfun&uppic=$uppic'>修改图片</A>";			  
			  }else
			  {
	           echo "<A HREF='?app=$this->type&act=upfile&edit=$edit&tf=$tf&picurl=$newfile&id=$id&jsfun=$jsfun&uppic=$uppic'>修改文件</A>";			  
			  }

	    echo "<script>
				if(self==top){
					window.opener.$jsfun('$newfile','$uppic');
					window.self.close();
				}else{
					window.parent.$jsfun('$newfile','$uppic');
				}
		     </script>";
				
	    exit;		
		
		}
        $this->assign('id', $id);
	    $this->assign('app', $this->type);		    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);		
        $this->assign('site_url',SITE_URL);		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
        $this->display('upfile.view.php');
    }

	
}


?>